package com.ruoyi.web.consumer;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.liangshi.domain.BizBsOrder;
import com.ruoyi.liangshi.domain.BizKcKsc;
import com.ruoyi.liangshi.service.IBizBsOrderService;
import com.ruoyi.liangshi.service.IBizKcKscService;
import com.ruoyi.liangshi.utils.NoTimeArchItemUtil;
import com.ruoyi.web.dto.BizBsDto;
import com.ruoyi.web.dto.KcKscDto;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.Date;


@Slf4j
@Service
@AllArgsConstructor
public class OrderReceiveConsumer {

    private final String DATA_PUSH_EXCHANGE = "data.sender.exchange";

    private final String DATA_ORDER_TOPIC = "data.sender.order.topic";

    private final String DATA_KSC_TOPIC = "data.sender.ksc.topic";

    private final IBizBsOrderService bizBsOrderService;

    private final IBizKcKscService bizKcKscService;

    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = DATA_ORDER_TOPIC, autoDelete = "false", durable = "true"), exchange = @Exchange(value = DATA_PUSH_EXCHANGE, durable = "true", type = ExchangeTypes.TOPIC), key = (DATA_ORDER_TOPIC))}, concurrency = "5-8")
    private void orderDataReceiver(String message) {
        Object object = JSON.parse(message);
        if (object instanceof JSONArray) {
            log.info("BizBsDto 是数组对象信息");
            return;
        }
        JSONObject jsonObject = (JSONObject) JSON.parse(message);
        if (jsonObject.get("createShopId") == null) {
            return;
        }
        String shopId = jsonObject.get("createShopId").toString();
        BizBsDto bizBsDto = jsonObject.toJavaObject(BizBsDto.class);
        Long id = bizBsDto.getId();
        BizBsOrder bizBsOrder = bizBsOrderService.getById(id);
        if (ObjectUtil.isNull(bizBsOrder)) {
            bizBsOrder = new BizBsOrder();
        }
        BeanUtil.copyProperties(bizBsDto, bizBsOrder);
        bizBsOrder.setShopId(Long.valueOf(shopId));
        bizBsOrderService.saveOrUpdate(bizBsOrder);
    }

    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = DATA_KSC_TOPIC, autoDelete = "false", durable = "true"), exchange = @Exchange(value = DATA_PUSH_EXCHANGE, durable = "true", type = ExchangeTypes.TOPIC), key = (DATA_KSC_TOPIC))}, concurrency = "5-8")
    private void kscDataReceiver(String message) {
        Object object = JSON.parse(message);
        if (object instanceof JSONArray) {
            log.info("KcKscDto 是数组对象信息");
            return;
        }
        JSONObject jsonObject = (JSONObject) JSON.parse(message);
        Long itemId = jsonObject.getLong("itemId");
        Long kitchenFlg = jsonObject.getLong("kitchenFlg");
        Date callUpTime = jsonObject.getDate("callUpTime");
        Date serveFinishTime = jsonObject.getDate("serveFinishTime");
        // 得到菜品的开始时间和实际上菜时间的时间间隔，作为菜品的加工时长
        Long timeLim = 0L;
        if (NoTimeArchItemUtil.noTimeArchItem(itemId) || kitchenFlg != 3) {
            // 检查菜品是否考核， 目前 米饭 打包盒  检查菜品状态完成才能继续
            timeLim = 1L;
        } else {
            timeLim = Math.abs(serveFinishTime.getTime() - callUpTime.getTime()) / 1000;
        }
        log.info("菜品的开始时间{}，菜品的完成时间{},时间间隔{}", callUpTime, serveFinishTime, timeLim);
        KcKscDto kcKscDto = jsonObject.toJavaObject(KcKscDto.class);
        Long id = kcKscDto.getId();
        BizKcKsc bizKcKsc = bizKcKscService.getById(id);
        if (ObjectUtil.isNull(bizKcKsc)) {
            bizKcKsc = new BizKcKsc();
        }
        BeanUtil.copyProperties(kcKscDto, bizKcKsc);
        bizKcKsc.setOrderCallTime(timeLim.intValue());
        bizKcKscService.saveOrUpdate(bizKcKsc);
    }


}
